<template>
    <ul class="list">
      <li class="item"
          v-for ="(item,key) in cities"
          :key="key" :ref="key"
          @click="handLetterClick"
          @touchstart="handleTouchStart"
          @touchmove="handleTouchMove"
          @touchend="handleTouchEnd"
      >{{key}}</li>

    </ul>
</template>
<script>
export default {
  name: 'CityAlphabet',
  props: {
    cities: Object
  },
  data () {
    return {
      handleStatus: false,
      startY: 0,
      timer: null
    }
  },
  computed: {
    letters () {
      const letters = []
      for (var i in this.cities) {
        letters.push(i)
      }
      return letters
    }
  },
  updated () {
    this.startY = this.$refs['A'][0].offsetTop
    console.log('startY=' + this.startY)
  },
  methods: {
    handLetterClick (e) {
      console.log(e)
      this.$emit('change', e.target.innerText)
    },
    handleTouchStart () {
      this.handleStatus = true
    },
    handleTouchMove (e) {
      if (this.handleStatus) {
        // const startY = this.$refs.A[0].offsetTop
        const startY = this.startY
        const touchY = e.touches[0].clientY - 80
        const index = Math.floor((touchY - startY) / 20)
        if (this.timer) {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          console.log(startY)
          console.log(touchY)
          if (index >= 0 && index < 25) {
            this.$emit('change', this.letters[index])
          }
        }, 16)
      }
    },
    handleTouchEnd () {
      this.handleStatus = false
    }
  }
}
</script>
<style scoped lang="stylus">
  @import '~styles/variables.styl'
.list
  display:flex
  flex-direction: column
  justify-content:center
  position:absolute;
  top:1.58rem
  right:0;
  bottom:0;
  padding:0.2rem 0
  background:none
  .item
    text-align:center
    color:$bgColor
    line-height:.4rem
</style>
